$(window).load(function () {
var h=window.location.hash;
if (h=="#!about") {
	$.colorbox({href:'static/about.html'});
}else if (h=="#!signup") {
	$.colorbox({href:'static/signup.html', "onComplete" : function () { $(".window input.uname").focus(); }});
}else if (h=="#!login") {
	$.colorbox({href:'static/login.html', "onComplete" : function () { $(".window input.uname").focus(); }});
}
})


$("a.about").colorbox({
	"href" : "static/about.html"
}, function () {
	window.location.hash="#!about";
});

$('#top-nav a').click(function () {
	var c=$(this).attr("class").split(" "), href="static/"+c[0]+".html";
	$.colorbox({"href" : href, "onComplete" : function () { $(".window input.uname").focus(); }});
});

$("#upload-form").submit(indicate_upload);

$("div.upload .sub").mouseover(function () {
	$("div.upload .browse").addClass("hover");
});

$("div.upload .sub").mouseout(function () {
	$("div.upload .browse").removeClass("hover");
});

$("div.upload .sub").mousedown(function () {
	$("div.upload .browse").removeClass("hover");
	$("div.upload .browse").addClass("active");
});

$(document).mouseup(function () {
	if ($("div.upload .browse").hasClass("active")) {
		$("div.upload .browse").removeClass("active");
	}
});

$("div.upload .sub").change(function () {
	if ($(document).find("#upload-frame").length!=1) {
		displayError ("Cannot upload the subtitle! Contact the admin!");
		$(this).removeAttr("val");
		return false;
	}
	var v=$(this).val(), l=v.length, ext=v.substr(l-4, l-1);
	if (ext!=".srt") {
		displayError ("Only .srt (SubRip) files are supported by now.");
		//$(this).removeAttr("val");
		return false;
	}
	$("#upload-form").submit();
});

$(".errors .close").live("click", function () {
	$(this).parent().remove();
	if ($("#errors li").length==0) {
		$("#errors").remove();
	}
});

$("#upload-frame").load(function () {
	$("div.uploading").remove();
	var res=$(this).contents().text();
	if ((res!="")&&(res!=null)) {
		res=jQuery.parseJSON (res);
		if (res.successful=="false") {
			displayError (res.message);
		}else if (res.successful=="true") {
			var url='http://subbies.therat.gr/sub/?s='+res.id, e_url=encodeURIComponent(url), bl_url, subli='<li><a href="'+url+'">'+res.name+'</a></li>';
			if ($("#subs").length==0) {
				$("body").append('<ul id="subs"></ul>');
				$("#subs").append(subli);
			}else{
				$(subli).insertBefore("#subs li:first");
			}
			/*$.get("http://api.bitly.com/v3/shorten?login=pkasid&apiKey=R_c6674bf7f67523123bc1f75f27763b2d&longUrl="+e_url+"&format=json", function (r) {
				if (r.data.url) {
					$("#subs a:replace").attr("href", r.data.url);
				};
			});*/
		}
	}
});

//File API Start
holder = document.body;

holder.ondragover = function () {
	$(this).css("background", "lightblue");
	var html='<div class="drop">Drop anywhere to upload!</div>';
	if ($("div.drop").length==0) {
		if ($("#subs").length==0) {
			$("body").append(html);
		}else{
			$(html).insertBefore("#subs");
		}
	}
	return false;
};

holder.ondragend = function () {
	$("div.drop").remove();
	$(this).css("background", "transparent");
};

holder.ondragleave = function () {
	$("div.drop").remove();
	$(this).css("background", "transparent");
};

holder.ondrop = function (e) {
	$(this).css('background', 'transparent');
	$("div.drop").remove();
	e.preventDefault();
	var file = e.dataTransfer.files[0], reader = new FileReader(), subs = new Object ();
	reader.onload = function (event) {
		subs.contents = reader.result;
		subs.name = file.name;
		if (file.size>500000) {
			displayError ("Your file is too big!");
			return false;
		}
		var l=file.name.length, ext=file.name.substr(l-4, l-1);
		if (ext!=".srt") {
			displayError ("Only .srt (SubRip) files are supported by now.");
			return false;
		}
		indicate_upload ();
		$.post("upload.php", {"fname" : subs.name, "contents" : subs.contents}, function (data) {
			$("div.uploading").remove();
			var res=jQuery.parseJSON(data);
			if (res.successful=="false") {
				displayError (res.message);
			}else if (res.successful=="true") {
				var url='http://subbies.therat.gr/sub/?s='+res.id, e_url=encodeURIComponent(url), bl_url, subli='<li><a href="'+url+'">'+res.name+'</a></li>';
				if ($("#subs").length==0) {
					$("body").append('<ul id="subs"></ul>');
					$("#subs").append(subli);
				}else{
					$(subli).insertBefore("#subs li:first");
				}
			/*$.get("http://api.bitly.com/v3/shorten?login=pkasid&apiKey=R_c6674bf7f67523123bc1f75f27763b2d&longUrl="+e_url+"&format=json", function (r) {
				if (r.data.url) {
					$("#subs a:replace").attr("href", r.data.url);
				};
			});*/
			}
		});
	};
	reader.readAsText(file);

	return true;
};
//File API Stop

function login (uname) {
$("#top-nav").html('<a class="dashboard" href="/dashboard" title="My Dashboard">Dashboard</a> <a class="myaccount" href="#account" title="My Account">'+uname+'</a>');
}

function indicate_upload () {
var html='<div class="uploading">Uploading subtitle... Please wait.</div>';
if ($("#subs").length==0) {
	$("body").append(html);
}else{
	$(html).insertBefore("#subs");
}
}

function displayError (error, conf, callback) {
	var appendTo="body", eheight, ewidth, efsize, eborder;
	if (typeof conf == "object") {
		if (conf.appendTo!=null) {
			appendTo=conf.appendTo;
		}
		
	}else if ((typeof conf == "function") && (typeof callback =="undefined")) {
		callback=conf;
	}
	if ($(appendTo).find(".errors").length==0) {
		$(appendTo).append('<ul class="errors"></ul>');
	}
	if ($(appendTo).find(".errors li").length==0) {
		$(appendTo).find(".errors").append("<li></li>");
	}else{
		$("<li></li>").insertBefore($(appendTo).find(".errors li:first"));
	}
	if (typeof conf == 'object') {
		if (typeof conf.fontsize != 'undefined') {
			$(appendTo).find(".errors li:first").css("font-size", conf.fontsize);
		}
		if (typeof conf.width != 'undefined') {
			$(appendTo).find(".errors li:first").css("width", conf.width);
		}
		if (typeof conf.height != 'undefined') {
			$(appendTo).find(".errors li:first").css("height", conf.height);
		}
		if (typeof conf.padding != 'undefined') {
			$(appendTo).find(".errors li:first").css("padding", conf.padding);
		}
		if (typeof conf.onDisappear == 'function') {
			var onDisappear = conf.onDisappear;
		}
	}
	
	if (typeof callback == "function") {
		callback.call();
	}

	
	$(".errors li:first").text(error).append('<div class="close"></div>').delay("5000").fadeOut("slow", function () {
		$(this).remove();
		if ($("#errors li").length==0) {
			$("#errors").remove();
		}
		if (typeof onDisappear == 'function') {
			onDisappear.call();
		}
	});
}
